package com.spring.framework.config.event;

import com.spring.framework.model.learn.ReflectWarning;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;

/**
 * 异步事件
 */
@EnableAsync
@Configuration
@ComponentScan(basePackages = {"com.spring.framework.config.event"})
public class MainConfig extends ReflectWarning {


	/**
	 * 往SimpleApplicationEventMulticaster设置taskExecutor则为异步事件或者使用
	 */
	@Async
	// @Bean(name = "applicationEventMulticaster")
	public SimpleApplicationEventMulticaster simpleApplicationEventMulticaster() {
		SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster();
		eventMulticaster.setTaskExecutor(new SimpleAsyncTaskExecutor());
		return eventMulticaster;
	}
	public static void main(String[] args) {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MainConfig.class);
		//下单
		Order order = new Order();
		order.setId(1);
		System.out.println("下单");

		ctx.publishEvent(new OrderEvent(order, "减库存"));
		System.out.println("日志...");
	}

}
